#include <baseClass/GLog.h>
#include<iostream>
#include<chrono>
#include<windows.h>

#ifdef GLOG_ENABLE 

void GLog::LogthreadMain()
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    while (true)
    {
        auto log = logQueue.Getfront();
        logQueue.pop();
        if(log.type == GLog::LogType::None)
        {
            SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
        }
        else if(log.type == GLog::LogType::Debug)
        {
            SetConsoleTextAttribute(hConsole, 11);
        }
        else if(log.type == GLog::LogType::Trace)
        {
            SetConsoleTextAttribute(hConsole, 14);
        }
        else if(log.type == GLog::LogType::Info)
        {
            SetConsoleTextAttribute(hConsole, 10);
        }
        else if(log.type == GLog::LogType::Warn)
        {
            SetConsoleTextAttribute(hConsole, 14);
        }else if(log.type == GLog::LogType::Error)
        {
            SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
        }
        
        
        std::cout << log.log << std::endl;
        SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
    }
    
}


SafeQueue<GLog::LogStruct> GLog::logQueue;
std::thread GLog::logThread(GLog::LogthreadMain);
#endif

void GLog::Log(const std::string log,const std::string& file, int line, enum LogType type)
{
#ifdef GLOG_ENABLE
    std::string logs = "[" +file+":"+ std::to_string(line) + "] " + log;
    GLog::LogStruct logStruct = {type, logs};
    logQueue.push(logStruct);
#endif
}
// #include <cstdarg>
// void GLog::Log_printf(const std::string& file,enum LogType type, int line,const char * Formt, ...)
// {
// #ifdef GLOG_ENABLE
//     char buffer[512];
//     va_list args;
//     va_start(args, Formt);
//     int n = sprintf("[%s:%d] ", file.c_str(), line);
//     vsprintf(buffer+n, Formt, args);
//     va_end(args);
//     std::string logs(buffer);

//     GLog::LogStruct logStruct = {type, logs};
//     logQueue.push(logStruct);
// #endif
// }
